package xyz.fuqufyb.aec.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.fuqufyb.aec.dao.RoleMenuMapper;
import xyz.fuqufyb.aec.entity.RoleMenuKey;
import xyz.fuqufyb.aec.service.IRoleMenuService;

import java.util.List;

/**
 * @author 用户: fyb
 * @description class: RoleMenuServiceImpl
 * @date 时间: 2021/1/30 19:56
 */
@Service("IRoleMenuService")
public class RoleMenuServiceImpl implements IRoleMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    /**
     * 批量修改  ，
     *
     * @param roleId
     * @param menuIds
     * @return
     */
    @Transactional //开启事务，防止数据丢失2
    @Override
    public int updateRoleMenuById(Integer roleId, List<Integer> menuIds) {

        if (roleId == 0 && menuIds.isEmpty()) {
            return 0;
        }
        List<RoleMenuKey> roleById = roleMenuMapper.getRoleById(roleId);

        if (!roleById.isEmpty()){
            /**
             * 先删除 原有的数据
             */
            roleMenuMapper.deleteByRoleId(roleId);
        }

        /**
         *批量修改
         */
        int roleMenuId = roleMenuMapper.updateRoleMenu(roleId, menuIds);
        if (roleMenuId > 0) {
            return roleId;
        }

        return 0;
    }
}
